package com.ixuestar.controller;

import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONObject;
import com.ixuestar.service.IxuestarTeacherService;
import com.ixuestar.util.ReturnResult;
import com.ixuestar.wxutil.GetwxOpenId;
import com.ixuestar.wxutil.PayConfigUtil;

@CrossOrigin
@RestController
@RequestMapping(value = "/smallAppTeacher",method = RequestMethod.POST)
public class IxuestarTeacherController {

	@Autowired
	private IxuestarTeacherService smallAppTeacherService;
	
	private Logger logger = Logger.getLogger(IxuestarTeacherController.class);
	
	
	/**
	 * @method 通过手机号获取老师信息
	 * @param jsonObject
	 * @param response
	 * @return
	 */
	@RequestMapping("/getWxSessionKey")
	@ResponseBody
	public Map<String, Object> getWxSessionKey(@RequestBody JSONObject jsonObject,HttpServletResponse response){
		//创建返回参数
		Map<String, Object> map = null;
		try {
			//获取appId和sec
			String appId = PayConfigUtil.ixuestarwx_APP_ID_X;
			String appSec = PayConfigUtil.ixuestarwx_APP_SEC_X;
			logger.info("-----------进入getWxSessionKey接口-------------");
			String code = jsonObject.getString("code");
			//执行接口
			JSONObject wxSessionKey = GetwxOpenId.getWxSessionKey(code,appId,appSec);
			//
			map = ReturnResult.correctResult(wxSessionKey);
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			map = ReturnResult.wrongResult();
		}
		return map;
	}
	
	/**
	 * @method 通过手机号获取老师信息
	 * @param jsonObject
	 * @param response
	 * @return
	 */
	@RequestMapping("/getWxTelephone")
	@ResponseBody
	public Map<String, Object> getWxTelephone(@RequestBody JSONObject jsonObject,HttpServletResponse response){
		//创建返回参数
		Map<String, Object> map = null;
		try {
			logger.info("-----------进入getWxTelephone接口-------------");
			String wxIv = jsonObject.getString("wxIv");
			String wxSessionKey = jsonObject.getString("wxSessionKey");
			String encryptedData = jsonObject.getString("encryptedData");
			//执行接口
			final JSONObject wxTelephone = GetwxOpenId.getWxTelephone(encryptedData, wxSessionKey, wxIv);
			logger.info("--------wxTelephone-"+wxTelephone+"----------");
			map = ReturnResult.correctResult(wxTelephone);
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			map = ReturnResult.wrongResult();
		}
		return map;
	}
	
	/**
	 * @method 通过手机号获取老师信息
	 * @param jsonObject
	 * @param response
	 * @return
	 */
	@RequestMapping("/getTeacherInfo")
	@ResponseBody
	public Map<String, Object> getTeacherInfo(@RequestBody JSONObject jsonObject,HttpServletResponse response){
		//创建返回参数
		Map<String, Object> map = null;
		try {
			logger.info("-----------进入getTeacherInfo接口-------------");
//			String ixstTelephone = jsonObject.getString("ixstTelephone");
//			logger.info("-----------进入getWxTelephone接口-------------");
			String wxIv = jsonObject.getString("wxIv");
			String wxSessionKey = jsonObject.getString("wxSessionKey");
			String encryptedData = jsonObject.getString("encryptedData");
			//执行接口
			final JSONObject wxTelephone = GetwxOpenId.getWxTelephone(encryptedData, wxSessionKey, wxIv);
			logger.info("--------wxTelephone-"+wxTelephone+"----------");
			if (wxTelephone.containsKey("phoneNumber")) {
				String ixstTelephone = wxTelephone.getString("phoneNumber");
				//执行接口
				JSONObject schoolTeacher = smallAppTeacherService.getSchoolTeacher(ixstTelephone);
				//判断code
				Integer integer = schoolTeacher.getInteger("code");
				if (integer==0) {
					schoolTeacher.remove("code");
					//返回参数
					map = ReturnResult.correctResult(schoolTeacher);
				}else {
					map = ReturnResult.otherResult(1, "手机号有误，请核对后重新登录");
				}
			}else {
				map = ReturnResult.otherResult(2, "手机号解析失败");
			}
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			map = ReturnResult.wrongResult();
		}
		return map;
	}
	
	/**
	 * @method 获取该年级的考试列表
	 * @param jsonObject
	 * @param response
	 * @return
	 */
	@RequestMapping("/getExaminationinfo")
	@ResponseBody
	public Map<String, Object> getExaminationinfo(@RequestBody JSONObject jsonObject,HttpServletResponse response){
		//返回参数
		Map<String, Object> map = null;
		try {
			logger.info("-----------进入getExaminationinfo接口-------------");
			String ixstId = jsonObject.getString("ixstId");
			//执行接口
			JSONObject examinationinfo = smallAppTeacherService.getExaminationinfo(ixstId);
			//返回参数
			map = ReturnResult.correctResult(examinationinfo);
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			map = ReturnResult.wrongResult();
		}
		return map;
	}
	
	/**
	 * @method 获取老师的权限、科目、年级
	 * @param jsonObject
	 * @param response
	 * @return
	 */
	@RequestMapping("/getExaminationSubjectClass")
	@ResponseBody
	public Map<String, Object> getExaminationSubjectClass(@RequestBody JSONObject jsonObject,HttpServletResponse response){
		//返回参数
		Map<String, Object> map = null;
		try {
			logger.info("-----------进入getExaminationSubjectClass接口-------------");
			//获取传递参数
			String ixeId = jsonObject.getString("ixeId");
			String ixstId = jsonObject.getString("ixstId");
			//执行接口
			JSONObject schoolTeacherInfo = smallAppTeacherService.getSchoolTeacherInfo(ixstId, ixeId);
			//返回参数
			map = ReturnResult.correctResult(schoolTeacherInfo);
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			map = ReturnResult.wrongResult();
		}
		return map;
	}
	
	/**
	 * @method 获取考试成绩
	 * @param jsonObject
	 * @param response
	 * @return
	 */
	@RequestMapping("/getTestScoresInfo")
	@ResponseBody
	public Map<String, Object> getTestScoresInfo(@RequestBody JSONObject jsonObject,HttpServletResponse response){
		//返回容器
		Map<String, Object> map = null;
		try {
			//获取参数
			String ixesId = jsonObject.getString("ixesId");
			String ixscId = jsonObject.getString("ixscId");
			//执行结果
			JSONObject testScoresInfo = smallAppTeacherService.getTestScoresInfo(ixesId, ixscId);
			//返回
			map = ReturnResult.correctResult(testScoresInfo);
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			map = ReturnResult.wrongResult();
		}
		return map;
	}
	
	/**
	 * @method 获取成绩区间的人数
	 * @param jsonObject
	 * @param response
	 * @return
	 */
	@RequestMapping("/getExaminationResultSection")
	@ResponseBody
	public Map<String, Object> getExaminationResultSection(@RequestBody JSONObject jsonObject,HttpServletResponse response){
		//返回map
		Map<String, Object> map = null;
		try {
			//接收参数
			String ixesId = jsonObject.getString("ixesId");
			String ixscId = jsonObject.getString("ixscId");
			//接口请求
			JSONObject examinationResultSection = smallAppTeacherService.getExaminationResultSection(ixesId, ixscId);
			//请求返回
			map = ReturnResult.correctResult(examinationResultSection);
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			map = ReturnResult.wrongResult();
		}
		return map;
	}
	
	/**
	 * @method 获取科目题目的情况
	 * @param jsonObject
	 * @param response
	 * @return
	 */
	@RequestMapping("/getExaminationQuestion")
	@ResponseBody
	public Map<String, Object> getExaminationQuestion(@RequestBody JSONObject jsonObject,HttpServletResponse response){
		//返回map
		Map<String, Object> map = null;
		try {
			//接收参数
			String ixesId = jsonObject.getString("ixesId");
			String ixscId = jsonObject.getString("ixscId");
			//接口请求单项选择
			JSONObject singleChoiceQuestion = smallAppTeacherService.getSingleChoiceQuestion(ixesId, ixscId);
			//接口请求多项选择
			JSONObject multipleChoiceQuestion = smallAppTeacherService.getMultipleChoiceQuestion(ixesId, ixscId);
			//机器口请求主观题
			JSONObject subjectivequestions = smallAppTeacherService.getSubjectivequestions(ixesId, ixscId);
			//添加对象
			JSONObject jsObject = new JSONObject(); 
			jsObject.put("singleChoiceQuestion", singleChoiceQuestion);
			jsObject.put("multipleChoiceQuestion", multipleChoiceQuestion);
			jsObject.put("subjectivequestions", subjectivequestions);
			//请求返回
			map = ReturnResult.correctResult(jsObject);
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			map = ReturnResult.wrongResult();
		}
		return map;
	}
	
	/**
	 * @method 获取学生答题的详细情况
	 * @param jsonObject
	 * @param response
	 * @return
	 */
	@RequestMapping("/getQuestionScore")
	@ResponseBody
	public Map<String, Object> getQuestionScore(@RequestBody JSONObject jsonObject,HttpServletResponse response){
		//返回map
		Map<String, Object> map = null;
		try {
			logger.info("----------getQuestionScore接口------------");
			//接收参数
			String ixesId = jsonObject.getString("ixesId");
			String ixeqId = jsonObject.getString("ixeqId");
			String ixscId = jsonObject.getString("ixscId");
			//接口请求单项选择
			JSONObject questionScore = smallAppTeacherService.getQuestionScore(ixesId, ixscId, ixeqId);
			//请求返回
			map = ReturnResult.correctResult(questionScore);
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			map = ReturnResult.wrongResult();
		}
		return map;
	}
}
